package com.xh.ssm.controller;

import com.xh.ssm.bean.Userinfo;
import com.xh.ssm.service.UserinfoService;
import com.xh.ssm.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author PangJunjie
 * @Date 2024/1/26/026
 */
@Controller
public class BaseController {
    @Autowired
    private UserinfoService userinfoService;

    @PostMapping("/login")
    @ResponseBody
    public R login(Userinfo userinfo, HttpSession session) {
        userinfo = userinfoService.login(userinfo);
        if (userinfo != null) {
            if (userinfo.getStatus() == 1) {
                session.setAttribute("loginInfo", userinfo);
                return R.ok();
            }
            return R.error("该用户已被封禁！");
        }
        return R.error("用户名或密码不正确！");
    }

    @GetMapping("/home")
    public String home() {
        return "home";
    }

    @GetMapping("/welcome")
    public String welcome() {
        return "welcome";
    }

    @GetMapping("/nologin")
    public String noLogin() {
        return "nologin";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginInfo");
        return "redirect:/";
    }
}
